package com.deer.wms.ASN.manage.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.deer.wms.ASN.manage.constant.AsnManageConstant;
import com.deer.wms.ASN.manage.dao.PackBillMapper;
import com.deer.wms.ASN.manage.model.in.AcceptRecord;
import com.deer.wms.ASN.manage.model.pack.PackBill;
import com.deer.wms.ASN.manage.model.pack.PackBillBO;
import com.deer.wms.ASN.manage.service.AcceptRecordService;
import com.deer.wms.ASN.manage.service.PackBillService;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.constant.InventoryConstant;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.model.Inventory.InventoryTransact;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.inventory.service.InventoryTransactService;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.StringUtils;
import com.deer.wms.system.manage.constant.SystemManageConstant;
import com.deer.wms.system.manage.service.CodeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : wei
 * @since : 2021-10-27 15:02:58
 **/
@Service
@Transactional
public class PackBillServiceImpl extends SuperServiceImpl<PackBillMapper, PackBill> implements PackBillService {

    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private InventoryTransactService inventoryTransactService;
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private AcceptRecordService acceptRecordService;

    @Override
    public List<Inventory> pack(PackBillBO packBillBO, Inventory acceptInv, CurrentUser currentUser) {
        List<Inventory> inventories = new ArrayList<>();
        AcceptRecord acceptRecord = acceptRecordService.getOneByField("accept_record_code", packBillBO.getAcceptRecordCode());

        for (int i = 0; i <= packBillBO.getNumber(); i++) {
            //判断是否是尾数
            if (i == packBillBO.getNumber()) {
                if (packBillBO.getLastQty().equals(0.0)) {
                    continue;
                }
                if (!packBillBO.getPrintLast()) {
                    continue;
                }
                packBillBO.setPackQty(packBillBO.getLastQty());
            }
            //生成库存事务
            InventoryTransact inventoryTransact = this.toStockRequest(packBillBO, acceptInv, currentUser);
            //事务提交
            int inventoryId = inventoryTransactService.run(inventoryTransact);
            //更新库存事务
            inventoryTransact.setToInventoryId(inventoryId);
            inventoryTransactService.updateById(inventoryTransact);

            Inventory inventory = inventoryService.getById(inventoryId);
            inventories.add(inventory);

            acceptRecord.setPackedQuantity(acceptRecord.getPackedQuantity() + inventory.getQuantity());
        }
        acceptRecordService.updateById(acceptRecord);
        return inventories;
    }

    @Override
    public List<Inventory> splitCt(PackBillBO packBillBO, Inventory inv, CurrentUser currentUser) {
        List<Inventory> inventories = new ArrayList<>();

        for (int i = 0; i < packBillBO.getNumber(); i++) {
            //生成库存事务
            InventoryTransact inventoryTransact = this.toStockRequest(packBillBO, inv, currentUser);
            //事务提交
            int inventoryId = inventoryTransactService.run(inventoryTransact);
            //更新库存事务
            inventoryTransact.setToInventoryId(inventoryId);
            inventoryTransactService.updateById(inventoryTransact);

            Inventory inventory = inventoryService.getById(inventoryId);
            inventories.add(inventory);
        }
        return inventories;
    }

    @Override
    public Inventory combCt(Inventory srcInv, List<Inventory> combInves, CurrentUser currentUser) {
        //FIXME: 目前只是简单的处理了库存
        double totalQty = 0.0;
        for (Inventory combInve : combInves) {
            totalQty += combInve.getQuantity();
            inventoryService.removeById(combInve);
        }
        srcInv.setQuantity(srcInv.getQuantity() + totalQty);
        inventoryService.updateById(srcInv);
        return srcInv;
    }

    private InventoryTransact toStockRequest(PackBillBO packBillBO, Inventory acceptInv, CurrentUser currentUser) {
        InventoryTransact inventoryTransact = new InventoryTransact();
        //Step1.准备数据
        inventoryTransact.setFromInventoryId(acceptInv.getInventoryId());
        //仓库
        inventoryTransact.setWareId(acceptInv.getWareId());
        inventoryTransact.setWareName(acceptInv.getWareName());
        //货主
        inventoryTransact.setToOrganizationId(acceptInv.getOrganizationId());
        inventoryTransact.setToOrganizationName(acceptInv.getOrganizationName());
        //库位
        inventoryTransact.setToCellCode(acceptInv.getCellCode());
        inventoryTransact.setFromBoxCode(acceptInv.getBoxCode());
        //需要生成唯一的追踪号
        String lpn = codeRuleService.generateCodeByRedis(SystemManageConstant.CODE_RULE_TN);
        inventoryTransact.setToBoxCode(lpn);
        //物料
        inventoryTransact.setItemCode(acceptInv.getItemCode());
        inventoryTransact.setItemName(acceptInv.getItemName());
        //批次
        inventoryTransact.setToBatchId(acceptInv.getBatchId());
        inventoryTransact.setToBatchName(acceptInv.getBatchName());
        //数量
        inventoryTransact.setFromQuantity(acceptInv.getQuantity());
        inventoryTransact.setToQuantity(packBillBO.getPackQty());
        //包装 转换率
        inventoryTransact.setToPackDetailId(acceptInv.getPackDetailId());
        inventoryTransact.setToPackDescribe(acceptInv.getPackDescribe());
        inventoryTransact.setToTransRatio(acceptInv.getTransRatio());
        //创建人 时间
        inventoryTransact.setCreateUserId(currentUser.getUserId());
        inventoryTransact.setCreateUserName(currentUser.getUserName());
        inventoryTransact.setCreateTime(DateUtil.now());
        //事务类型
        inventoryTransact.setTransactType(InventoryConstant.TRANSACT_TYPE_PACK);
        //来源单据号
        inventoryTransact.setBillNo(packBillBO.getAcceptRecordCode());

        AcceptRecord acceptRecord = acceptRecordService.getOneByField("accept_record_code", packBillBO.getAcceptRecordCode());
        if(ObjectUtil.isNotEmpty(acceptRecord)){
            if(acceptRecord.getQcState().equals(1)){
                //质检标识
                inventoryTransact.setState(InventoryConstant.TRANSACT_STATE_UNNORMAL);
                inventoryTransact.setStateDis("待检");
            }else{
                inventoryTransact.setState(InventoryConstant.TRANSACT_STATE_NORMAL);
            }
        }else{
            inventoryTransact.setState(InventoryConstant.TRANSACT_STATE_NORMAL);
        }




        //Step2.保存，返回
        inventoryTransactService.save(inventoryTransact);
        return inventoryTransact;
    }
}
